<?php
/**
 * 人员数据模型
 * 
 * @package    apps
 * @version    1.0
 * @author     Ray
 * @license    MIT License
 * @copyright  2010 - 2014 PMonkey Team
 * @link       http://wangxiaolei.cn
 */

class Model_People extends \Orm\Model
{

	/**
	 * @var  string  table name to overwrite assumption
	 */
	protected static $_table_name = 'peoples';

	protected static $_primary_key = array('user_id');

	/**
	 * @var array	defined observers
	 */
	protected static $_observers = array(
		'Orm\\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'property' => 'created_at',
			'mysql_timestamp' => false
		),
		'Orm\\Observer_UpdatedAt' => array(
			'events' => array('before_update'),
			'property' => 'updated_at',
			'mysql_timestamp' => false
		),
		'Orm\\Observer_Typing' => array(
			'events' => array('after_load', 'before_save', 'after_save')
		)
	);


	/**
	 * @var array	has_one relationships
	 */
	protected static $_has_one = array(
		'user' => array(
			'model_to' => 'Model\\Auth_User',
			'key_from' => 'user_id',
			'key_to'   => 'id',
		)
	);

	/**
	 * @var array	has_many relationships
	 */
	protected static $_has_many = array(	
		'wechats' => array(
			'model_to' => 'Model_Wechat',
			'key_from' => 'user_id',
			'key_to'   => 'user_id',
		),
		'photoalbum' => array(
			'model_to' => 'Model_PeoplesAttachment',
			'key_from' => 'user_id',
			'key_to'   => 'people_id',
			'cascade_save' => true
			//'cascade_delete' => true
		)		
	);
	/*
	protected static $_belongs_to = array(
		'photoalbum' => array(
	        'key_from' => 'user_id',
	        'model_to' => 'Model_PeoplesAttachment',
	        'key_to' => 'people_id',
	        'cascade_save' => true,
	        'cascade_delete' => true,
	    )
	);

	protected static $_eav = array(
        'photoalbum' => array(
            'attribute' => 'key',//thumb
            'value' => 'value',//id
        ),
    );*/

	/**
	 * @var array	many_many relationships
	 */
	/*protected static $_many_many = array(
		'roles' => array(
			'key_from' => 'id',
			'model_to' => 'Model\\Auth_Role',
			'key_to' => 'id',
			'table_through' => null,
			'key_through_from' => 'group_id',
			'key_through_to' => 'role_id',
		),
		'permissions' => array(
			'key_from' => 'id',
			'model_to' => 'Model\\Auth_Permission',
			'key_to' => 'id',
			'table_through' => null,
			'key_through_from' => 'group_id',
			'key_through_to' => 'perms_id',
		),
	);*/

	public static function do_create($data)
	{
		$entity = Model_People::forge($data);
		if(isset($data['photoalbum'])){
			$entity->photoalbum = array();
			foreach ($data['photoalbum'] as $value) {
				array_push($entity->photoalbum, \Model_PeoplesAttachment::forge($value));				
			}
		}
		if($entity->save()){
			return array('ret' => 'succ', 'msg' => '保存成功', 'data' => $entity);
		}else{
			return array('ret' => 'err', 'msg' => '保存失败');
		}
	}

	public static function do_update($id, $data)
	{
		$entity = Model_People::find($id)->set($data);		
		if(isset($data['photoalbum'])){
			$entity->photoalbum = array();
			foreach ($data['photoalbum'] as $value) {
				array_push($entity->photoalbum, \Model_PeoplesAttachment::forge($value));				
			}
		}
		
		if($entity->save()){
			return array('ret' => 'succ', 'msg' => '保存成功', 'data' => $entity);
		}else{
			return array('ret' => 'err', 'msg' => '保存失败');
		}
	}

	/**
	 * before_insert observer event method
	 */
	public function _event_before_insert()
	{
		// assign the user id that lasted updated this record
		//$this->user_id = ($this->user_id = \Auth::get_user_id()) ? $this->user_id[1] : 0;
	}

	/**
	 * before_update observer event method
	 */
	public function _event_before_update()
	{
		$this->_event_before_insert();
	}
}
